package com.sanjiahantang.ythshopping.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.config.SPConfig;
import com.sanjiahantang.ythshopping.model.Suppidmsg;
import com.sanjiahantang.ythshopping.model.UserInforModel;
import com.sanjiahantang.ythshopping.presenter.SQPaySuppidPresenter;
import com.sanjiahantang.ythshopping.presenter.UserInforPresenter;
import com.sanjiahantang.ythshopping.utils.SPUtils;
import com.sanjiahantang.ythshopping.views.SQPaySuppidView;
import com.sanjiahantang.ythshopping.views.UserInforView;

public class SQpayFristActivity extends Activity implements SQPaySuppidView,UserInforView,View.OnClickListener{

    TextView tv_title;
    EditText et_payprice;
    Button btn_next;
    ImageView iv_back;


    String suppId;
    SQPaySuppidPresenter sqpaysuppidpresenter;
    UserInforPresenter userInforPresenter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqpay_frist);
        suppId =getIntent().getStringExtra("suppid");


    }

    @Override
    protected void onResume() {
        super.onResume();

        ConfigValue.DATA_KEY = (String) SPUtils.get(SQpayFristActivity.this, SPConfig.KEY, "");
        userInforPresenter=new UserInforPresenter(SQpayFristActivity.this);
        if (ConfigValue.DATA_KEY != null && !ConfigValue.DATA_KEY.equals("")) {
            //本地存在key
            /*mFrameLayoutLogin.setVisibility(View.VISIBLE);
            pullRefreshLayout2.setVisibility(View.GONE);*/
            userInforPresenter.loadInfor(this);


        } else {
            //不存在key，提示需要登录

            Intent intent = new Intent(SQpayFristActivity.this, LoginActivity.class);
            startActivity(intent);
        }

    }

    public void InitView()
    {
        tv_title= (TextView) findViewById(R.id.title);
        et_payprice= (EditText) findViewById(R.id.et_SQF_payprice);
        btn_next= (Button) findViewById(R.id.btn_SQF_next);
        iv_back= (ImageView) findViewById(R.id.back);

        btn_next.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        sqpaysuppidpresenter=new SQPaySuppidPresenter(this);
        sqpaysuppidpresenter.setSqSuppid(this,suppId);


    }

    @Override
    public void getSuppidMessage(Suppidmsg response) {
        if (response.getCode()==1)
        {
            tv_title.setText(response.getData().getTitle());
        }else {
            Toast.makeText(this,response.getMsg(),Toast.LENGTH_LONG).show();
            finish();
        }



    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btn_SQF_next:
                if (et_payprice.getText().equals(""))
                {
                    Toast.makeText(this,"支付金额不能为空",Toast.LENGTH_LONG).show();
                }else if (Double.valueOf(et_payprice.getText().toString())<0.01)
                {
                    Toast.makeText(this,"支付金额不能为0",Toast.LENGTH_LONG).show();
                } else{
                    Intent intentnext=new Intent(SQpayFristActivity.this, SQPaySecondActivity.class);
                    intentnext.putExtra("suppid",suppId);
                    intentnext.putExtra("price",et_payprice.getText().toString());
                    intentnext.putExtra("title",tv_title.getText().toString());
                    startActivity(intentnext);
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void inforData(UserInforModel model) {
        if (model.getCode().equals("1")) {
            ConfigValue.uInfor = model.getData().get(0);
            InitView();
        } else {
            if (model.getCode().equals("-220")) {
                SPUtils.remove(this, SPConfig.KEY);
            }
            Intent intent = new Intent(SQpayFristActivity.this, LoginActivity.class);
            startActivity(intent);
        }
    }
}
